﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NF.Perceptron
{
    class Program
    {
        public class Shuffler : IComparer<double[]>
        {
            #region IComparer<double[]> Members

            public int Compare(double[] x, double[] y)
            {
                return R.Next(2) - 1;
            }

            #endregion

            private Random R = new Random();
        }

        private const int Iterations = 253452;

        static void Main(string[] args)
        {
            var data = ReadData(args[0]);

            foreach (var i in data)
            {
                if (i[2] == 2.0)
                {
                    int j;
                    for (j = 0; j < i.Length - 1; ++j)
                        i[j] *= -1;
                    i[j] = -1;
                }
            }

            int n = data[0].Length;
            double[] W = new double[n];
            bool wtf;

            Shuffler sh = new Shuffler();

            foreach (var itm in data)
            {
                WriteArray(itm);
            }

            Console.WriteLine();

            do
            {
                wtf = false;
                foreach (var X in data/*.OrderBy(x => x, sh)*/)
                {
                    if (Dot(W, X) <= 0.0)
                    {
                        for (int i = 0; i < n; ++i)
                        {
                            W[i] += X[i];
                        }

                        wtf = true;
                    }
                }
            } while (wtf);

            WriteArray(W, n);
        }

        private static void WriteArray(double[] arr)
        {
            WriteArray(arr, arr.Length);
        }

        private static void WriteArray(double[] arr, int n)
        {
            for (int i = 0; i < n; ++i)
            {
                if (i > 0)
                    Console.Write(',');
                Console.Write(arr[i]);
            }

            Console.WriteLine();
        }

        private static double Dot(double[] a, double[] b)
        {
            double v = 0.0;
            for (int i = 0; i < a.Length; ++i)
                v += (a[i] * b[i]);

            return v;
        }

        private static List<double[]> ReadData(String filename)
        {
            using (var r = File.OpenText(filename))
            {
                List<double[]> items = new List<double[]>();
                double[] tmp;
                String line;
                String[] s;

                line = r.ReadLine();
                s = line.Split(' ');
                int m = int.Parse(s[0]);
                int n = int.Parse(s[1]);
                int k = int.Parse(s[2]);

                while ((line = r.ReadLine()) != null)
                {
                    s = line.Split(' ');
                    if (s.Length != n)
                        throw new Exception();
                    tmp = new double[s.Length];

                    for (int i = 0; i < s.Length; ++i)
                        tmp[i] = double.Parse(s[i], System.Globalization.CultureInfo.InvariantCulture.NumberFormat);

                    items.Add(tmp);
                }

                return items;
            }
        }
    }
}
